﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using service;
using model;
namespace Trathat
{
    public partial class ChitietSanphamTrathat : System.Web.UI.Page
    {
        string loai = "";
        string nhom = "";
        string ten;
        protected void Page_Load(object sender, EventArgs e)
        {
            loai = Request.QueryString["Loai"];
            string id = Request.QueryString["ID"];
            

            List<SanphamTrathatModel> lisSanpham = new List<SanphamTrathatModel>();
            List<LoaiSanphamTrathat> listLoaisanpham = new List<LoaiSanphamTrathat>();
            lisSanpham = new SanphamTrathatService().getListSanphamByID(Convert.ToInt64(id));
            listLoaisanpham = new LoaisanphamService().getListLoaiSanphamByID(Convert.ToInt64(loai));
            if (listLoaisanpham.Count > 0)
            {
                nhom = listLoaisanpham[0].IDNhomsanpham + "";
                ten = listLoaisanpham[0].Tenloaisanpham;
                if (listLoaisanpham[0].IDNhomsanpham == 1)
                    lnkNhom.Text = " > Trà cụ";
                else
                    lnkNhom.Text = " > Trà";
                lnkLoai.Text = " > " + ten;
                lblSanpham.Text = " > " + lisSanpham[0].Tensanpham;
            }
            

            listSanpham.DataSource = new SanphamTrathatService().getListSanphamByID(Convert.ToInt64(id));
            listSanpham.DataBind();
            getistSanphamKhac(loai, id);
            getListImageBySanpham(id);
        }

        private void getistSanphamKhac(string s, string id)
        {
            List<SanphamTrathatModel> lisSP = new List<SanphamTrathatModel>();
            lisSP = new SanphamTrathatService().getListSanphamByLoai(Convert.ToInt64(s));
            for (int i=0; i < lisSP.Count; i++)
            {
                if (lisSP[i].ID == Convert.ToInt64(id))
                {
                    lisSP.RemoveAt(i);
                    break;
                }
            }
            RadListView1.DataSource = lisSP;
            RadListView1.DataBind();
        }

        private void getListImageBySanpham(string id)
        {
            listHinhanh.DataSource = new ImageService().getListImageBySanpham(Convert.ToInt64(id));
            listHinhanh.DataBind();
        }

        protected void lnkLoai_Click(object sender, EventArgs e)
        {
            Response.Redirect("SanphamTrathat.aspx?Nhom=" + nhom + "&&Loai= " + loai + "&&Ten=" + ten);
        }

        protected void lnkNhom_Click(object sender, EventArgs e)
        {
            Response.Redirect("Nhomsanpham.aspx?Nhom=" + nhom);
        }
    }
}